package cn.demengsite.service.impl;

import cn.demengsite.dao.MemberDAO;
import cn.demengsite.dao.OrderDAO;
import cn.demengsite.model.BaseInfo;
import cn.demengsite.model.Member;
import cn.demengsite.model.Order;
import cn.demengsite.model.Report;
import cn.demengsite.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDAO memberDAO;

    @Autowired
    private OrderDAO orderDAO;


    @Override
    public BaseInfo getMemberByCardOrMobile(String word) {

        BaseInfo baseInfo;
        Member member ;
        if (word.length()==11) {
            //如果是11位，那当做手机来处理
            member = memberDAO.getMemberByMobile(word);

        }else {
            member = memberDAO.getMemberByCard(word);
        }

        if (member != null) {
            baseInfo = new BaseInfo("0","查询会员成功",member);
        }else {
            baseInfo = new BaseInfo("100","查询会员失败");
        }


        return baseInfo;
    }

    @Override
    public BaseInfo addNewMember(Member member) {
        if (member.getMemberName() == null) {
            member.setMemberName("待添加");
            member.setCardName("德蒙钻石卡");
            member.setMemberDiscount(0.38);

        }
        memberDAO.addNewMember(member);
        return new BaseInfo("0","成功添加会员",member);
    }

    @Override
    public BaseInfo updateMember(Member member) {
        memberDAO.updateMember(member);
        return new BaseInfo("0","成功更新会员",member);
    }

    @Override
    public BaseInfo memberRecharge(Double rechargeValue, Integer id) {
        memberDAO.memberRecharge(rechargeValue,id);
        //order表中写入充值记录

        Order order = new Order();
        order.setCashPay(rechargeValue);
        order.setMemberId(id);
        order.setOrderStatus(3);
        order.setTotalPrice(rechargeValue);
        order.setTotalNum(0);
        orderDAO.insertNewOrder(order);
        return new BaseInfo("0","成功充值");
    }

    @Override
    public BaseInfo getMemberSale(Integer id) {
       List<Report> reportList= orderDAO.getMemberSale(id);
       return new BaseInfo("0","成功查询会员消费",reportList);
    }
}
